import unittest

class AppTestBase(unittest.TestCase):
    NO_RESULTS_ERROR = "Could not find %s because there were no results."
    MORE_THAN_ONE_RESULTS_ERROR = "More than one message of type %s was found."

    def assertContainsMsg(self, result, msg):
        if isinstance(result, list):
            self.assertTrue(msg in result)
        else:
            self.assertEquals(result, msg)

    def get_one_message(self, results, msg_type):
        self.assertNotEquals(results, None, self.NO_RESULTS_ERROR % msg_type)
        self.assertNotEquals(results, [], self.NO_RESULTS_ERROR % msg_type)

        if isinstance(results, list):
            msgs = filter(lambda x: type(x) == msg_type, results)
            self.assertEquals(1, len(msgs),
                    self.MORE_THAN_ONE_RESULTS_ERROR % msg_type)

            return msgs[0]

        else:
            self.assertEquals(type(results), msg_type)
            return results

    def assertDoesNotContainMsg(self, result, msg):
        self.assertTrue(result is None or msg not in result)

    def assertContainsNoMsg(self, result):
        self.assertTrue(result == [] or result is None)
